package com.jyuxuan.recruitment.controller;

import com.jyuxuan.recruitment.pojo.Enterprise;
import com.jyuxuan.recruitment.pojo.EnterpriseLogin;
import com.jyuxuan.recruitment.service.impl.EnterpriseServiceImpl;
import com.jyuxuan.recruitment.utils.DataMap;
import com.jyuxuan.recruitment.utils.JsonResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
@CrossOrigin
@RequestMapping("/enter")
public class EnterpriseController {

    @Autowired
    private EnterpriseServiceImpl enterpriseService;

    /**
     * 查看企业信息-企业端
     *
     * @return
     */
    @PostMapping("viewInfo")
    @ResponseBody
    public String viewInfo() {
        EnterpriseLogin enterpriseLogin = (EnterpriseLogin) SecurityUtils.getSubject().getPrincipal();
        String number = enterpriseLogin.getNumber();
        DataMap data = enterpriseService.enterpriseView(number);
        return JsonResult.build(data).toJSON();
    }

    /**
     * 查看企业信息-管理端
     *
     * @return
     */
    @PostMapping("viewEnters")
    @ResponseBody
    public String viewEnters(Enterprise enterprise) {
        DataMap data = enterpriseService.enterpriseList(enterprise);
        return JsonResult.build(data).toJSON();
    }

    /**
     * 填写企业信息
     *
     * @param enterprise
     * @return
     */
    @PostMapping("fillInfo")
    @ResponseBody
    public String fillInfo(Enterprise enterprise) {
        EnterpriseLogin enterpriseLogin = (EnterpriseLogin) SecurityUtils.getSubject().getPrincipal();
        String number = enterpriseLogin.getNumber();
        enterprise.setNumber(number);
        enterprise.setState(0);
        DataMap data = enterpriseService.enterpriseFill(enterprise);
        return JsonResult.build(data).toJSON();
    }

    /**
     * 修改企业信息
     *
     * @param enterprise
     * @return
     */
    @PostMapping("modifyInfo")
    @ResponseBody
    public String modifyInfo(Enterprise enterprise) {
        EnterpriseLogin enterpriseLogin = (EnterpriseLogin) SecurityUtils.getSubject().getPrincipal();
        String number = enterpriseLogin.getNumber();
        enterprise.setNumber(number);
        enterprise.setState(0);
        DataMap data = enterpriseService.enterpriseModify(enterprise);
        return JsonResult.build(data).toJSON();
    }

    /**
     * 上传营业执照
     *
     * @param file
     * @return
     *
     */
    @PostMapping("uploadLicense")
    @ResponseBody
    public String uploadLicense(@RequestParam("file") MultipartFile file) { //license改为file解决后标识号为的405的错误，解决后台报错的is not present
        EnterpriseLogin enterpriseLogin = (EnterpriseLogin) SecurityUtils.getSubject().getPrincipal();
        String number = enterpriseLogin.getNumber();
        DataMap data = enterpriseService.uploadLicense(file, number);
        return JsonResult.build(data).toJSON();
    }

    /**
     * 审核企业信息
     *
     * @param enterpriseId
     * @param state
     * @return
     */
    @PostMapping("auditInfo")
    @ResponseBody
    public String auditInfo(int enterpriseId, int state) {
        DataMap data = enterpriseService.enterpriseAudit(enterpriseId, state);
        return JsonResult.build(data).toJSON();
    }

}
